/**
 * Copyright (C) 2009-2012, KingHool.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses.
 */
package com.kinghool.smodel.io.bean.xml;

import java.util.List;
import java.util.Map;

import com.kinghool.ebos.commons.utils.Utils;
import com.kinghool.smodel.io.bean.annotation.Array;
import com.kinghool.smodel.io.bean.annotation.Attribute;
import com.kinghool.smodel.io.bean.annotation.Bean;
import com.kinghool.smodel.io.bean.annotation.Exclude;

/**
 * 
 * @Author Allen Deng(dengtailin@gmail.com) 
 */
@Bean(name="bean")
public class BeanModel {
	@Attribute(name="include-all")
	private boolean includeAll = true;

	@Attribute(name="class-name")
	private String className;

	@Attribute(name="name")
	private String name;

	@Array(container="fields", child="attribute")
	private List<AttributeBeanFieldModel> attributes = Utils.newList();

	@Array(container="fields", child="text")
	private List<TextBeanFieldModel> texts = Utils.newList();
	
	@Array(container="fields", child="child")
	private List<ChildBeanFieldModel> children = Utils.newList();
	
	@Array(container="fields", child="array")
	private List<ArrayElementBeanFieldModel> arrays = Utils.newList();
	
	@Array(container="fields", child="auto")
	private List<AutoBeanFieldModel> autos = Utils.newList();

	@Exclude
	private Map<String, BeanFieldModel> beanFieldsCache = Utils.newMap();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isIncludeAll() {
		return includeAll;
	}

	public void setIncludeAll(boolean includeAll) {
		this.includeAll = includeAll;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}
  
	public BeanFieldModel getBeanField(String fieldName) {
		return beanFieldsCache.get(fieldName);
	} 
	
	public BeanFieldModel[] getBeanFields() {
		return beanFieldsCache.values().toArray(new BeanFieldModel[0]);
	} 

	public List<AttributeBeanFieldModel> getAttributes() {
		return attributes;
	}

	public void setAttributes(List<AttributeBeanFieldModel> attributes) {
		this.attributes = attributes;
		for (BeanFieldModel bf : attributes) {
			beanFieldsCache.put(bf.getFieldName(), bf);
		}
	}
	
	public void addAttribute(AttributeBeanFieldModel attribute) {
		this.attributes.add(attribute);
		beanFieldsCache.put(attribute.getFieldName(), attribute);
	}

	public List<TextBeanFieldModel> getTexts() {
		return texts;
	}

	public void setTexts(List<TextBeanFieldModel> texts) {
		this.texts = texts;
		for (BeanFieldModel bf : texts) {
			beanFieldsCache.put(bf.getFieldName(), bf);
		}
	}
	
	public void addText(TextBeanFieldModel text) {
		this.texts.add(text);
		beanFieldsCache.put(text.getFieldName(), text);
	}

	public List<ChildBeanFieldModel> getChildren() {
		return children;
	}

	public void setChildren(List<ChildBeanFieldModel> children) {
		this.children = children;
		for (BeanFieldModel bf : children) {
			beanFieldsCache.put(bf.getFieldName(), bf);
		}
	}
	
	public void addChild(ChildBeanFieldModel child) {
		this.children.add(child);
		beanFieldsCache.put(child.getFieldName(), child);
	}

	public List<ArrayElementBeanFieldModel> getArrays() {
		return arrays;
	}

	public void setArrays(List<ArrayElementBeanFieldModel> arrays) {
		this.arrays = arrays;
		for (BeanFieldModel bf : arrays) {
			beanFieldsCache.put(bf.getFieldName(), bf);
		}
	}
	
	public void addArray(ArrayElementBeanFieldModel array) {
		this.arrays.add(array);
		beanFieldsCache.put(array.getFieldName(), array);
	}

	public List<AutoBeanFieldModel> getAutos() {
		return autos;
	}

	public void setAutos(List<AutoBeanFieldModel> autos) {
		this.autos = autos;
		for (BeanFieldModel bf : autos) {
			beanFieldsCache.put(bf.getFieldName(), bf);
		}
	}
	
	public void addAuto(AutoBeanFieldModel array) {
		this.autos.add(array);
		beanFieldsCache.put(array.getFieldName(), array);
	}
	
	public void addBeanField(BeanFieldModel bfModel) {
		if (bfModel instanceof AttributeBeanFieldModel) {
			this.addAttribute((AttributeBeanFieldModel) bfModel);
		} else if (bfModel instanceof TextBeanFieldModel) {
			this.addText((TextBeanFieldModel) bfModel);
		} else if (bfModel instanceof ChildBeanFieldModel) {
			this.addChild((ChildBeanFieldModel) bfModel);
		} else if (bfModel instanceof ArrayElementBeanFieldModel) {
			this.addArray((ArrayElementBeanFieldModel) bfModel);
		} else if (bfModel instanceof AutoBeanFieldModel) {
			this.addAuto((AutoBeanFieldModel) bfModel);
		}		
	}
}
